package com.yanqu.road.dao.impl.travelbook;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.travelbook.UserTravelAlbum;
import com.yanqu.road.entity.travelbook.UserTravelCard;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author : Cxp
 * @Time : 2024-11-13
 * @Description : 用户旅行册Dao
 */

public class TravelBookUserAlbumDaoImpl extends TempDataObjectDao<UserTravelAlbum> {
    public static String getAlbumSql = "select * from t_u_travel_album where userId=?";

    public Map<Integer, UserTravelAlbum> getAlbumMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, UserTravelAlbum> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAlbumSql);
            int i = 1;
            call.setLong(i, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                int albumId = rs.getInt("albumId");
                res.put(albumId, parseData(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return res;
    }

    public static String getBeautySql = "select * from t_u_travel_album where userId=?";

    public Map<Integer, List<UserTravelAlbum>> getBeautyMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UserTravelAlbum>> res = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBeautySql);
            int i = 1;
            call.setLong(i, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserTravelAlbum userTravelAlbum = parseData(rs);
                int beautyId = userTravelAlbum.getBeautyId();
                if (!res.containsKey(beautyId)) {
                    res.put(beautyId, new ArrayList<>());
                }
                res.get(beautyId).add(userTravelAlbum);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return res;
    }

    public static String updateAlbumSql = "update `t_u_travel_album` set level=?,cards=? where userId=? and albumId=?";

    @Override
    protected boolean updateData(UserTravelAlbum userTravelAlbum) {
        userTravelAlbum.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, userTravelAlbum.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTravelAlbum.getCards())));
        para.put(i++, new DbParameter(Types.BIGINT, userTravelAlbum.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTravelAlbum.getAlbumId()));
        return getDb().executeNoneQuery(updateAlbumSql, para) > 0;
    }

    public static String insertAlbumSql = "insert into `t_u_travel_album`(userId,albumId,beautyId,level,cards) values (?,?,?,?,?)";

    @Override
    protected boolean addData(UserTravelAlbum userTravelAlbum) {
        userTravelAlbum.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.BIGINT, userTravelAlbum.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTravelAlbum.getAlbumId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTravelAlbum.getBeautyId()));
        para.put(i++, new DbParameter(Types.INTEGER, userTravelAlbum.getLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userTravelAlbum.getCards())));
        return getDb().executeNoneQuery(insertAlbumSql, para) > 0;
    }

    @Override
    protected UserTravelAlbum parseData(ResultSet rs) throws SQLException {
        UserTravelAlbum userTravelAlbum = new UserTravelAlbum();
        userTravelAlbum.setUserId(rs.getLong("userId"));
        userTravelAlbum.setBeautyId(rs.getInt("beautyId"));
        userTravelAlbum.setAlbumId(rs.getInt("albumId"));
        userTravelAlbum.setLevel(rs.getInt("level"));
        userTravelAlbum.setCards(JSON.parseObject(rs.getString("cards"), new TypeReference<List<UserTravelCard>>() {}));
        return userTravelAlbum;
    }
}
